package cn.no7player.util;


import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author eajon
 * @description 文件 bytes 互转
 * @date 2019-08-26 11:31
 */
public class ByteUtils {


    //Since JDK 7, NIO
    public static void writeBytesToFileNio(byte[] bFile, String fileDest) {

        try {
            Path path = Paths.get(fileDest);
            Files.write(path, bFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //Since JDK 7, NIO
    public static void writeBytesToFileNio(InputStream inputStream, String fileDest) {

        try {
            Path path = Paths.get(fileDest);
            Files.write(path, IOUtils.toByteArray(inputStream));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static byte[] readBytesFromFileNio(File file) {
        try {
            return Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }


}
